package user.zc.excel.render;

import user.zc.excel.data.info.ICell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.List;

class SheetRender implements ICreate<XSSFRow,List<ICell.CellInfo[]>> {


    private String name;
    private XSSFWorkbook workbook;
    private Integer index = 0;
    private XSSFSheet sheet;

    private Integer freeRows;
    public SheetRender(XSSFWorkbook workbook, String name,Integer freeRows){
        this.name = name;
        this.workbook = workbook;
        this.freeRows = freeRows;
    }
    @Override
    public void draw(List<ICell.CellInfo[]> data) {
        sheet =  workbook.createSheet(name);
        //需要多做一个操作，设置列宽
        for (int i=0;i<data.get(0).length;i++) {
            sheet.setColumnWidth(i,data.get(0)[i].getStyleInfo().getWidth());
        }
        data.remove(0);
        for (ICell.CellInfo[] cellInfo : data) {
            if(cellInfo!=null && cellInfo.length>0){
                new RowRender(next()).draw(cellInfo);
            }
        }

        //冻结
        sheet.createFreezePane(0, freeRows); // 冻结第一行，参数为水平和垂直偏移量
    }


    @Override
    public XSSFRow next() {
        return sheet.createRow(index++);
    }

}
